package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.entity.*;
import com.itheima.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 * @Auther: 宋H
 * @Date: 2022/9/15
 * @Description: com.itheima.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/order")
@Api(tags="订单信息相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> orderList(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单信息分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> orderById(int page, int pageSize,Long number,String beginTime,String endTime){
        log.info("分页查询中...");
        Page<Orders> orderPage = new Page<Orders>(page,pageSize);

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getNumber,number);
        //查询beginTime大于等于这个时间
        lqw.ge(beginTime!=null,Orders::getOrderTime,beginTime);
        lqw.le(endTime!=null,Orders::getOrderTime,endTime);
        lqw.orderByDesc(Orders::getOrderTime);

        orderService.page(orderPage,lqw);
        return R.success(orderPage);
    }

}
